package edu.osu.cse.c651.cryptochess;

import java.util.ArrayList;

/**
 * The Knight
 * 
 * @author Team Pawned
 * 
 */
public class Knight implements Piece {

	/**
	 * the Color of this Knight
	 */
	private Color color;

	/**
	 * the number of moves made by this Knight
	 */
	private int moveCount;

	/**
	 * Constructor
	 * 
	 * @param color
	 *            the Color of this Knight
	 */
	public Knight(Color color) {
		this.color = color;
		moveCount = 0;
	}

	/**
	 * 
	 */
	public boolean legalMove(int x1, int y1, int x2, int y2) {
		if ((x1 == x2) || (y1 == y2)) {
			return false;
		}
		int deltaX = Math.abs(x1 - x2);
		int deltaY = Math.abs(y1 - y2);
		int maxMovement = Math.max(deltaX, deltaY);
		int minMovement = Math.min(deltaX, deltaY);
		if ((maxMovement == 2) && (minMovement == 1)) {
			return true;
		}
		return false;
	}

	/**
	 * 
	 */
	public Color color() {
		return color;
	}

	/**
	 * 
	 */
	public PieceName name() {
		return PieceName.Knight;
	}

	/**
	 * 
	 */
	public void movesPlusOne() {
		moveCount += 1;
	}

	/**
	 * 
	 */
	public int moveCount() {
		return moveCount;
	}

	/**
	 * 
	 */
	public ArrayList<Coordinate> possibleMoves(ChessBoard board, int x, int y) {
		ArrayList<Coordinate> moves = new ArrayList<Coordinate>();

		if (((x + 1 < 8) && (y + 2 < 8))
				&& (board.getPiece(x + 1, y + 2) == null || board.getPiece(
						x + 1, y + 2).color() != this.color)) {
			Coordinate move = new Coordinate(x + 1, y + 2);
			moves.add(move);
		}
		if (((x + 1 < 8) && (y - 2 >= 0))
				&& (board.getPiece(x + 1, y - 2) == null || board.getPiece(
						x + 1, y - 2).color() != this.color)) {
			Coordinate move = new Coordinate(x + 1, y - 2);
			moves.add(move);
		}
		if (((x - 1 >= 0) && (y + 2 < 8))
				&& (board.getPiece(x - 1, y + 2) == null || board.getPiece(
						x - 1, y + 2).color() != this.color)) {
			Coordinate move = new Coordinate(x - 1, y + 2);
			moves.add(move);
		}
		if (((x - 1 >= 0) && (y - 2 >= 0))
				&& (board.getPiece(x - 1, y - 2) == null || board.getPiece(
						x - 1, y - 2).color() != this.color)) {
			Coordinate move = new Coordinate(x - 1, y - 2);
			moves.add(move);
		}
		if (((x + 2 < 8) && (y + 1 < 8))
				&& (board.getPiece(x + 2, y + 1) == null || board.getPiece(
						x + 2, y + 1).color() != this.color)) {
			Coordinate move = new Coordinate(x + 2, y + 1);
			moves.add(move);
		}
		if (((x + 2 < 8) && (y - 1 >= 0))
				&& (board.getPiece(x + 2, y - 1) == null || board.getPiece(
						x + 2, y - 1).color() != this.color)) {
			Coordinate move = new Coordinate(x + 2, y - 1);
			moves.add(move);
		}
		if (((x - 2 >= 0) && (y + 1 < 8))
				&& (board.getPiece(x - 2, y + 1) == null || board.getPiece(
						x - 2, y + 1).color() != this.color)) {
			Coordinate move = new Coordinate(x - 2, y + 1);
			moves.add(move);
		}
		if (((x - 2 >= 0) && (y - 1 >= 0))
				&& (board.getPiece(x - 2, y - 1) == null || board.getPiece(
						x - 2, y - 1).color() != this.color)) {
			Coordinate move = new Coordinate(x - 2, y - 1);
			moves.add(move);
		}

		return moves;
	}
}